﻿using System;
using RpcClient;
using RpcHelper;
using RpcManageClient;
using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;
namespace Wedonek.RpcStore.Service.Collect
{
        internal class RpcMerConfigCollect : BasicCollect<RpcMerConfigDAL>, IRpcMerConfigCollect
        {
                private IRpcServerCollect _Server => this.GetCollect<IRpcServerCollect> ();


                public RpcMerConfig[] GetConfigs (long rpcMerId)
                {
                        return this.BasicDAL.GetConfigs (rpcMerId);
                }
                public RpcMerConfig GetConfig (Guid id)
                {
                        RpcMerConfig config = this.BasicDAL.GetConfig (id);
                        if (config == null)
                        {
                                throw new ErrorException ("rpc.mer.config.not.find");
                        }
                        return config;
                }

                public Guid Add (AddMerConfig add)
                {
                        if (this.BasicDAL.CheckIsExists (add.RpcMerId, add.SystemTypeId))
                        {
                                throw new ErrorException ("rpc.mer.config.exists");
                        }
                        RpcMerConfig obj = add.ConvertMap<AddMerConfig, RpcMerConfig> ();
                        obj.Id = Tools.NewGuid ();
                        this.BasicDAL.Add (obj);
                        this._Server.RefreshMerConfig (add.RpcMerId, add.SystemTypeId);
                        return obj.Id;
                }

                public void Set (Guid id, SetMerConfig param)
                {
                        RpcMerConfig config = this.GetConfig (id);
                        this.BasicDAL.Set (id, param);
                        this._Server.RefreshMerConfig (config.RpcMerId, config.SystemTypeId);
                }
                public void Drop (Guid id)
                {
                        RpcMerConfig config = this.GetConfig (id);
                        if (config == null)
                        {
                                throw new ErrorException ("rpc.mer.config.not.find");
                        }
                        this.BasicDAL.Drop (id);
                        this._Server.RefreshMerConfig (config.RpcMerId, config.SystemTypeId);
                }
        }
}
